//
//  PopoverAnimator.swift
//  JMWB
//
//  Created by 个人电脑 on 16/3/19.
//  Copyright © 2016年 个人电脑. All rights reserved.
//

import UIKit

class PopoverAnimator: NSObject {
    var isPresented : Bool = false
    var presentedFrame : CGRect = CGRectZero
    var animateCallBack : ((isPresented : Bool) ->())?

}
extension PopoverAnimator:UIViewControllerTransitioningDelegate{
    
    // 目的:改变弹出View的尺寸和添加蒙版
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        let present = JMPresenrationController(presentedViewController: presented, presentingViewController: presenting)
        present.presentedFrame = presentedFrame
        return present
    }
    // 目的:弹出动画交给谁来管理
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = true
        if animateCallBack != nil{
            animateCallBack!(isPresented : isPresented)
        }
        return self
    }
    // 目的:消失动画交给谁来管理
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        if animateCallBack != nil{
            animateCallBack!(isPresented : isPresented)
        }
        return self
    }
}
extension PopoverAnimator:UIViewControllerAnimatedTransitioning{
    // 返回动画执行的时间间隔
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        isPresented ? presentedAnimate(transitionContext) : dismissedAnimate(transitionContext)
}
    /*
    // 在该方法中有一个参数transitionContext 通过transitionContext,可以获取到弹出的View,也可以获取到消失的View UITransitionContextFromViewKey : 获取消失的View UITransitionContextToViewKey : 获取弹出的View
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        if let presentedView = transitionContext.viewForKey(UITransitionContextToViewKey){
            
            transitionContext.containerView()?.addSubview(presentedView)
            
            presentedView.transform = CGAffineTransformMakeScale(1.0, 0.0)
            //设置锚点
            //            presentedView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
            presentedView.layer.anchorPoint = CGPointMake(0.5, 0)
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
                
                presentedView.transform = CGAffineTransformIdentity
                }) { (_) -> Void in
                    // 如果是自定义转场,动画结束后必须告诉转场上下文动画已经完成
                    transitionContext.completeTransition(true)
            }
        }else{
            let dismissedView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
                dismissedView.transform = CGAffineTransformMakeScale(1.0, 0.00001)
                }, completion: { (_) -> Void in
                    dismissedView.removeFromSuperview()
                    transitionContext.completeTransition(true)
            })        }
    }
*/
}
extension PopoverAnimator{
    //执行弹出动画
    private func presentedAnimate(transitionContext: UIViewControllerContextTransitioning){
    let presentedView = transitionContext.viewForKey(UITransitionContextToViewKey)!
            
    transitionContext.containerView()?.addSubview(presentedView)
    presentedView.transform = CGAffineTransformMakeScale(1.0, 0.0)
    presentedView.layer.anchorPoint = CGPointMake(0.5, 0)
    UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
        presentedView.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
    // 如果是自定义转场,动画结束后必须告诉转场上下文动画已经完成
    transitionContext.completeTransition(true)
    }
}
    //执行消失动画
    private func dismissedAnimate(transitionContext: UIViewControllerContextTransitioning){
        let dismissedView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            dismissedView.transform = CGAffineTransformMakeScale(1.0, 0.00001)
            }, completion: { (_) -> Void in
                dismissedView.removeFromSuperview()
                transitionContext.completeTransition(true)
        })
    }
}